﻿using System;
using System.Windows;
using System.Windows.Input;
using Oxage.Models;

namespace Oxage.Views
{
	public partial class FindWindow : Window
	{
		#region Constructor
		public FindWindow()
		{
			InitializeComponent();

			this.Loaded += (sender, e) =>
			{
				txtFind.Focus();
				txtFind.SelectAll();
			};

			this.Closing += (sender, e) =>
			{
				Hide();
				e.Cancel = true;
			};
		}
		#endregion

		#region Public properties
		public FindViewModel ViewModel
		{
			get
			{
				return this.DataContext as FindViewModel;
			}
		}
		#endregion

		#region Public methods
		public void FindNext()
		{
			OnActionRequest(FindWindowAction.Find, ViewModel);
		}
		#endregion

		#region Event handlers
		private void Window_KeyDown(object sender, KeyEventArgs e)
		{
			switch (e.Key)
			{
				case Key.F:
					if ((Keyboard.Modifiers & ModifierKeys.Alt) > 0)
					{
						OnActionRequest(FindWindowAction.Find, ViewModel);
					}
					break;

				case Key.R:
					if ((Keyboard.Modifiers & ModifierKeys.Alt) > 0)
					{
						OnActionRequest(FindWindowAction.Replace, ViewModel);
					}
					break;

				case Key.A:
					if ((Keyboard.Modifiers & ModifierKeys.Alt) > 0)
					{
						OnActionRequest(FindWindowAction.ReplaceAll, ViewModel);
					}
					break;

				case Key.Escape:
					Hide();
					break;
			}
		}

		private void Find_Click(object sender, RoutedEventArgs e)
		{
			OnActionRequest(FindWindowAction.Find, ViewModel);
		}

		private void Replace_Click(object sender, RoutedEventArgs e)
		{
			OnActionRequest(FindWindowAction.Replace, ViewModel);
		}

		private void ReplaceAll_Click(object sender, RoutedEventArgs e)
		{
			OnActionRequest(FindWindowAction.ReplaceAll, ViewModel);
		}

		private void Close_Click(object sender, RoutedEventArgs e)
		{
			Hide();
			//Close(); //Note: close cannot reuse instance
		}
		#endregion

		#region Static methods
		private static FindWindow window = null; //Last used window instance
		public static FindWindow LastDialog { get { return window; } }

		public static FindWindow ShowDialog(FindViewModel model, FindWindowCallback callback)
		{
			if (window == null)
			{
				window = new FindWindow();
				window.ActionRequest += (sender, e) =>
				{
					if (callback != null && !string.IsNullOrEmpty(window.ViewModel.FindText))
					{
						callback(e.Action, e.Result);
					}
				};
			}
			window.DataContext = model;
			window.Show();
			return window;
		}

		public static FindWindow ShowFindDialog(string findText, FindWindowCallback callback)
		{
			var model = new FindViewModel();
			//model.Mode = Find
			model.FindText = findText;
			return ShowDialog(model, callback);
		}

		public static FindWindow ShowReplaceDialog(string findText, string replaceText, FindWindowCallback callback)
		{
			var model = new FindViewModel();
			//model.Mode = Replace
			model.FindText = findText;
			model.ReplaceText = replaceText;
			return ShowDialog(model, callback);
		}
		#endregion

		#region Bubble events
		public event EventHandler<FindWindowEventArgs> ActionRequest;
		protected virtual void OnActionRequest(FindWindowAction action, FindViewModel model)
		{
			if (ActionRequest != null)
			{
				ActionRequest(this, new FindWindowEventArgs(action, model));
			}
		}
		#endregion
	}
}
